﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.Data.Factory;
using NXDO.WebAPI.Controllers;
using System;
using System.Collections.Generic;
using TDM.DbData.DataEntity;

namespace TDM.WebSvr.Controllers
{
    /// <summary>
    /// 激光试验任务结果文件表
    /// </summary>
    [AllowAnonymous]
    public class TestResultFileController : ApiDbEntityController<TestResultFile, long>
    {

        protected override string GetPkName()
        {
            return "TDFID";
        }

        protected override string GetTableName()
        {
            return "TestResultFile";
        }

        /// <inheritdoc/>
        public override TestResultFile Post([FromBody] TestResultFile data)
        {
            //获取用户信息
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            data.CreateUID = uid;
            data.CreateTime = DateTime.Now;
            return base.Post(data);
        }

        /// <inheritdoc/>
        public override bool Delete(long pkValue)
        {
            var sql = $"update {GetTableName()} set IsDelete = 1 where {GetPkName()} = {pkValue}";
            return DbHelper.SetUpdateCommand(sql).ExecuteNonQuery() > 0;
        }

        /// <summary>
        /// 获取试验结果的文件集合
        /// </summary>
        /// <param name="LaserResultID"></param>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<TestResultFile> GetTestResultFiles(long LaserResultID)
        {
            var sql = "select * from TestResultFile where LaserResultID = {0} and IsDelete = 0";
            return DbHelper.SetCommand(sql, LaserResultID).ExecuteEntity<TestResultFile>();
        }
    }
}
